//
//  NewFetureCollectionViewController.swift
//  weibopush
//
//  Created by 龚欣 on 15/6/26.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit
import FFAutoLayout

private let newFeaturereuseIdentifier = "NewFetureCell"

class NewFetureCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFetureCollectionViewCell.self, forCellWithReuseIdentifier: newFeaturereuseIdentifier)
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        flowLayout.itemSize = (collectionView?.bounds.size)!
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

   let flowLayout = UICollectionViewFlowLayout()
    
    init(){
        super.init(collectionViewLayout: flowLayout)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(newFeaturereuseIdentifier, forIndexPath: indexPath) as! NewFetureCollectionViewCell
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let indexpath = collectionView.indexPathsForVisibleItems().last!
        if indexpath.item == imageCount - 1 {
            print("开始动画")
           (collectionView.cellForItemAtIndexPath(indexpath) as! NewFetureCollectionViewCell).startAnimation()
            
        }
    }
    
}

class NewFetureCollectionViewCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        addSubview(startButton)
        
//        startButton.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
        startButton.ff_AlignInner(ff_AlignType.BottomCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -160))
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var iconView : UIImageView = {
            let iconV = UIImageView()
            iconV.frame = self.bounds
            return iconV
        }()
    
    var imageIndex = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
    lazy var startButton : UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        btn.addTarget(self, action: "startButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        }()

    func startButtonClicked(){
        NSNotificationCenter.defaultCenter().postNotificationName("SwitchRootViewControllerNotification", object: true)
    
    }
    
    func startAnimation(){
    
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    
}
